package com.corechina.apex_osswords.controller;


import com.corechina.apex_osswords.common.Response;
import com.corechina.apex_osswords.common.SysLog;
import com.corechina.apex_osswords.domain.entity.SysMenu;
import com.corechina.apex_osswords.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author cc
 * @since 2022-03-07
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    @ApiOperation("新增菜单")
//    @RequiresPermissions("sys:menu:add")
    @SysLog("新增菜单")
    @PostMapping("/add")
    public Response add(@RequestBody SysMenu sysMenu) {
        return iSysMenuService.add(sysMenu);
    }

    @ApiOperation("根据用户Id查询菜单信息")
    @GetMapping("/getMenuByUserId")
    public Response getMenuByUserId() {
        return iSysMenuService.getMenuByUserId();
    }

    @ApiOperation("查询菜单列表")
    @GetMapping("/getMenuAllList")
//    @RequiresPermissions("sys:menu:query")
    public Response getMenuAllList() {
        return iSysMenuService.getMenuAllList();
    }

    @ApiOperation("根据父级Id查询菜单信息")
//    @RequiresPermissions("sys:menu:query")
    @GetMapping("/getMenuByParentId")
    public Response getMenuByParentId(Integer parentId, String type) {
        return Response.ok(iSysMenuService.getMenuByParentId(parentId, type));
    }

    @ApiOperation("删除")
//    @RequiresPermissions("sys:menu:del")
    @SysLog("删除菜单")
    @DeleteMapping("/del")
    public Response del(Integer id) {
        List<SysMenu> sysMenus = iSysMenuService.getMenuByParentId(id, null);
        if (sysMenus.size() > 0) {
            return Response.error("该级存在子级，请先删除子级");
        }
        return iSysMenuService.del(id);
    }

    @ApiOperation("编辑")
//    @RequiresPermissions("sys:menu:update")
    @SysLog("编辑菜单")
    @PutMapping("/update")
    public Response update(@RequestBody SysMenu sysMenu) {
        return iSysMenuService.update(sysMenu);
    }

    @ApiOperation("获取详情")
//    @RequiresPermissions("sys:menu:query")
    @GetMapping("/getOne")
    public Response getOne(Integer id) {
        return iSysMenuService.getOne(id);
    }
}

